<?php
//扫码付费未登录情况走此脚本
header("content-type:text/html;charset=utf8");

$return = array();
if (isset($_GET['code'])) {
  //微信授权回调
  $parameter = $_GET['parameter'];
  $data = explode('-',$parameter);
  $appId = $data[0];
  $mid = $data[1];
  $code = $_GET['code'];
  $data = getToken($code);
  $user_info_data = getUserInfo($data['access_token'],$data['openid']); 
  $pdo = new PDO('mysql:host=123.57.16.97;dbname=project','root','zqVnUy6cxsQXbpEZ');
  $pdo->exec("set names utf8");
  $res = loginInfoHandle($pdo,$appId,$user_info_data);
  if ($res) {
    $uid = getLoginUid($pdo,$appId);
    $movieInfo = getMovieInfo($pdo,$mid);
    $movieInfo['user'] = getLoginInfo($pdo,$uid);
    $url = 'https://pay.hispot.cn/app/h5/movieDetails.php?data='.json_encode($movieInfo);
    header('location:'.$url);
    exit;
  }else{
    //获取登录信息失败
    header("location:https://pay.hispot.cn/app/h5/error.html");
  }
}else{
  //获取请求登录的应用端唯一标识
  $appId = $_GET['appId'];
  $mid = $_GET['mid'];
  $parameter = $appId.'-'.$mid;
  $url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2de38da107527240&redirect_uri=https://pay.hispot.cn/app/getWxInfo.php?parameter='.$parameter.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'; 
  header('location:'.$url);

}

function getLoginUid($pdo,$appId){
  $sql = "SELECT uid FROM `relation` WHERE appid = '".$appId."' AND status = 1";
  $uid = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
  return $uid['uid'];
}

//通过uid来获取登录信息
function getLoginInfo($pdo,$uid){
  $sql = "SELECT uid,nickname,headimgurl,openid FROM `user` WHERE uid = ".$uid;
  $userInfo = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
  return $userInfo;
}

//获取电影详情信息
function getMovieInfo($pdo,$mid){
  $sql = "SELECT mid,name,price,poster FROM movie WHERE mid = '".$mid."'";
  $movieInfo = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
  return $movieInfo;
}

//获取微信Token信息
function getToken($code){
  $token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx2de38da107527240&secret=446fdd6335e31f0ceeaf63562ff03a4c&code=".$code."&grant_type=authorization_code";
  $data = json_decode(file_get_contents($token_url),true);
  return $data;
}

//获取微信用户基本信息
function getUserInfo($access,$openid){
  $user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access."&openid=".$openid."&lang=zh_CN";
  $user_info_data = json_decode(file_get_contents($user_info_url),true);
  return $user_info_data;
}

//登录信息处理
function loginInfoHandle($pdo,$appId,$user_info_data){
  //检测到用户发起登录，将此appid的所有关联微信状态全部改为未登录
  resetLoginStatus($pdo,$appId);
  //获取uid
  $uid = getUid($pdo,$user_info_data['openid']);
  //判断是否存在uid,存在直接修改登录状态，不存在将此登录信息与appid关联入库
  if (empty($uid)) {
    $uid = insertUserInfo($pdo,$user_info_data['nickname'],$user_info_data['headimgurl'],$user_info_data['openid']);
    $res = insertRelation($pdo,$appId,$uid);
  }else{
    $res = updateLoginStatus($pdo,$uid,$appId);
  }
  return $res;
}

//此用户不存在添加数据信息入库
function insertUserInfo($pdo,$nickname,$headimgurl,$openid){
  $time = date("Y-m-d H:i:s");
  $sql = "INSERT INTO `user`(openid,nickname,headimgurl,`time`) VALUES('$openid','$nickname','$headimgurl','$time')";
  $pdo->exec($sql);
  return $pdo->lastInsertId();
}

//添加关联信息，并将状态改为已登录
function insertRelation($pdo,$appId,$uid){
  $time = date("Y-m-d H:i:s");
  $sql = "INSERT INTO `relation`(appid,uid,`time`,status) VALUES('$appId','$uid','$time','1')";
  $res = $pdo->exec($sql);
  return $res;
}

//获取此微信号基本信息的uid
function getUid($pdo,$openid){
  $sql = "SELECT uid FROM `user` WHERE openid = '".$openid."'";
  $uid = $pdo->query($sql)->fetch(PDO::FETCH_ASSOC);
  return $uid['uid'];
}

//修改登录状态信息
function updateLoginStatus($pdo,$uid,$appId){
  $selestSql = "SELECT rid FROM `relation` WHERE uid = '$uid' AND appid = '$appId'";
  $rid = $pdo->query($selestSql)->fetch(PDO::FETCH_ASSOC);
  if ($rid) {
    $sql = "UPDATE `relation` SET status = 1 WHERE uid = '$uid' AND appid = '$appId'";
    $res = $pdo->exec($sql);
  }else{
    $res = insertRelation($pdo,$appId,$uid);
  }
  return $res;
}

//检测到用户发起登录操作，将此appid的所有微信登录关联信息全部改成未登录
function resetLoginStatus($pdo,$appId){
  $sql = "UPDATE `relation` SET `status` = 0 WHERE appid = '".$appId."'";
  $pdo->exec($sql);
}
